import java.util.Arrays;

//自定义栈
public class MyStack {
    int array[];
    int size;
    public MyStack(){
        array=new int[3];
        size=0;
    }

    public int push(int data){
        ensureCapacity();
        array[size++]=data;
        return data;
    }
    public int size(){
        return size;
    }
    public int pop(){
        if(size==0){
            throw new StackIndexOutOfBoundsException("栈中没有元素,请重试!");
        }
        return array[--size];
    }
    public int peek(){
        if(size==0){
            throw new StackIndexOutOfBoundsException("栈中没有元素,请重试!");
        }
        return array[size-1];
    }
    public boolean isEmpty(){
        return size==0;
    }
    public void ensureCapacity(){
        if(size>=array.length){
            array= Arrays.copyOf(array,size*2);//源数组和加长的长度
        }
    }
    public void display(){
        for (int i = 0; i < size; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
}
